package com.service;

import com.controller.ShootGame;
import com.domain.*;

import java.awt.*;
import java.util.ArrayList;

public class FlyingsService {

    private ArrayList<Flying> flyings;//声明一个存储敌机和蜜蜂的数组
    private int number;//控制飞行物数量

    public FlyingsService() {
        this.flyings = new ArrayList<>();
    }

    public ArrayList<Flying> getFlyings() {
        return flyings;
    }

    /**画敌机和蜜蜂的方法**/
    public void paintFlyings(Graphics g){//g:一只画笔
        for (Flying flying : flyings) {
            //画敌机和蜜蜂
            g.drawImage(flying.getImage(),flying.getX(),flying.getY(),null);
        }
    }

    /**
     * 创建飞行物
     */
    public void  newFlyings(){
        //控制难度
        if(ShootGame.score > ShootGame.difficulty*100 && ShootGame.difficulty <= 30){
            ShootGame.difficulty++;//难度增加
            //创建大飞机
            Bigplane bigplane = new Bigplane();
            //大飞机血量增加
            bigplane.setLifr(bigplane.getLifr()*ShootGame.difficulty);
            flyings.add(bigplane);
        }
        //控制敌机出现数量
        if (number++ % ((30-ShootGame.difficulty)>0?30-ShootGame.difficulty:0) == 0){
            //创建敌机
            Airplane airplane = new Airplane();
            //敌机血量增加
            airplane.setLifr(airplane.getLifr()*ShootGame.difficulty);
            flyings.add(airplane);
        }
    }

    /**
     * 让所有的飞行物移动
     */
    public void moveAction(){
        //让所有敌机大飞机和蜜蜂移动
        for (Flying flying : flyings) {
            flying.move();
            //判断飞行物爆炸效果，移动四次后删除，
            if (flying instanceof Bom && ((Bom)flying).getIndex()%5 == 0){
                flyings.remove(flying);
                break;
            }
            //判断飞行物是否移动出窗体，是则删除，当敌机飞出窗口则分数减少并删除
            if (flying.getY() > ShootGame.HEIGHT){
                if (flying instanceof Airplane){
                    ShootGame.score-=3;
                }else if (flying instanceof Bigplane){
                    ShootGame.score-=5;
                }
                flyings.remove(flying);
                break;
            }
        }
    }


    /**
     * 英雄碰撞方法
     * //碰左    x < x2 + w2
     * //碰右    x + w > x2
     * //碰上    y < y2 +h2
     * //碰下    y + h > y2
     * @param hero
     * @return
     */
    public boolean crashAction(Hero hero){
        for (Flying flying : flyings) {
            //飞行物的坐标及宽高
            int x2 = flying.getX();
            int y2 = flying.getY();
            int w2 = flying.getWidth();
            int h2 = flying.getHeight();
            //判断是否发生碰撞
            if (hero.getX()+10 < x2+w2 && hero.getX()+hero.getWidth()-10 > x2 && hero.getY()+20 < y2+h2 && hero.getY()+hero.getHeight()-20 > y2){
                if (flying instanceof Bee){
                    //判断奖励类型
                    if (((Bee) flying).getAwardType()==1){
                        //限制火力
                        if (hero.getDoubleFire() < 8){
                            //火力增加
                            hero.setDoubleFire(hero.getDoubleFire()+1);
                        }else {
                            //生命增加
                            hero.setLifr(hero.getLifr()+1);
                        }
                    }else {
                        hero.setLifr(hero.getLifr()+1);//生命增加
                    }
                    //删除敌机
                    flyings.remove(flying);
                    //爆炸效果
//                    flyings.add(new Bom(x2,y2,w2,h2));
                    return true;
                }else {
                    if (flying instanceof Airplane){
                        if (hero.getDoubleFire() > 0){
                            hero.setDoubleFire(hero.getDoubleFire()-1);//火力减少
                            ShootGame.score-=3;//分数减少
                        }else {
                            hero.setLifr(hero.getLifr()-1);//生命减少
                            ShootGame.score-=3;//分数减少
                        }
                        //删除敌机
                        flyings.remove(flying);
                        //爆炸效果
                        flyings.add(new Bom(x2,y2,w2,h2));
                        return true;
                    }else if (flying instanceof Bigplane){
                        //限制火力
                        if (hero.getDoubleFire() > 0){
                            hero.setDoubleFire(hero.getDoubleFire()-1);//火力减少
                            hero.setLifr(hero.getLifr()-1);//生命减少
                            ShootGame.score-=5;//分数减少
                        } else {
                            hero.setLifr(hero.getLifr()-1);//生命减少
                            ShootGame.score-=5;//分数减少
                        }
                        //删除敌机
                        flyings.remove(flying);
                        //爆炸效果
                        flyings.add(new Bom(x2,y2,w2,h2));
                        return true;
                    }
                }
            }
        }
        return false;
    }
}
